﻿Imports DotNetNuke
Imports DotNetNuke.UI.Utilities

Imports Lucene.Net.Index
Imports Lucene.Net.Search
Imports Lucene.Net.Documents

Imports Aricie.DNN.Services
Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports Aricie.DNN.Modules.LuceneSearch.Logs


Namespace Aricie.DNN.Modules.LuceneSearch.UI

    Partial Public Class FieldListFilter
        Inherits LuceneFilterControlBase


#Region "Private Members"
        'Private _IsLogged As Boolean = False
        Private _fieldName As String
        Private _nbValues As Integer
        Private _idx As String
        Private _idex As Integer = -1
        Public Const glbFilterType As String = "FieldListFilter"
#End Region

#Region "Public Property"

        Public Property FieldName() As String
            Get
                Return _fieldName
            End Get
            Set(ByVal value As String)
                _fieldName = value
            End Set
        End Property

        Public Property NbValues() As Integer
            Get
                Return _nbValues
            End Get
            Set(ByVal value As Integer)
                _nbValues = value
            End Set
        End Property

        Private Property mySelectedValue() As String
            Get
                Return _idx
            End Get
            Set(ByVal value As String)
                _idx = value
            End Set
        End Property

        'Private Property IsLogged() As Boolean
        '    Get
        '        Return _IsLogged
        '    End Get
        '    Set(ByVal value As Boolean)
        '        _IsLogged = value
        '    End Set
        'End Property

        Private Property mySelectedIndex() As Integer
            Get
                Return _idex
            End Get
            Set(ByVal value As Integer)
                _idex = value
            End Set
        End Property


#End Region

        Public Sub New()
            QueryMode = UI.QueryMode.Filter
        End Sub


#Region "Event Handlers"

        Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
            If Not Me.IsPostBack Then
                lnkReset.Enabled = False
                Me.BindLayout()
            End If


        End Sub

        Private Sub lvListField_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles lvListField.ItemCommand
            mySelectedValue = e.CommandArgument.ToString
        End Sub



        Protected Sub lvListField_SelectedIndexChanging(ByVal sender As Object, ByVal e As EventArgs) Handles lvListField.SelectedIndexChanged
            mySelectedIndex = lvListField.SelectedIndex
            Me.OnFilterChanged()
            Me.BindLayout()
        End Sub

        Private Sub lnkReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkReset.Click
            mySelectedValue = "Reset"
            lvListField.SelectedIndex = -1
            Me.OnFilterChanged()
            Me.BindLayout()
        End Sub

#End Region



#Region "ILuceneFilterControl implements"


        Public Overrides Property Enabled() As Boolean
            Get
                Return Me.lvListField.Enabled
            End Get
            Set(ByVal value As Boolean)
                Me.lvListField.Enabled = value
            End Set
        End Property

        Public Overrides Function GetFieldNames() As IEnumerable(Of String)
            Return New String() {FieldName}
        End Function

        Public Overrides Function GetFilter() As LuceneFilterQuery
            Dim toReturn As New LuceneFilterQuery
            Dim fieldFilter As PhraseQuery = Nothing
            Dim fieldValue As String = ""
            If mySelectedValue IsNot Nothing Then
                fieldValue = mySelectedValue
            ElseIf lvListField.SelectedValue IsNot Nothing Then
                fieldValue = lvListField.SelectedValue.ToString
            End If

            fieldFilter = New PhraseQuery
            If Not fieldValue = "" Then
                Dim FieldTerm As New Term(FieldName, fieldValue)
                fieldFilter.Add(FieldTerm)
            End If

            toReturn.FilterQuery = fieldFilter
            Return toReturn
        End Function

        Public Overrides Function HasFilter() As Boolean

            If mySelectedValue IsNot Nothing Then
                If Not mySelectedValue = "Reset" Then : Return True
                Else : Return False
                End If
            ElseIf lvListField.SelectedValue IsNot Nothing Then
                If Not lvListField.SelectedValue.ToString = "Reset" Then : Return True
                Else : Return False
                End If
            End If
        End Function

        Public Overrides Function ParseFilter(ByVal objFilter As LuceneFilterQuery) As Boolean
            If TypeOf (objFilter.FilterQuery) Is TermQuery Then
                Dim objTerm As Term = DirectCast(objFilter.FilterQuery, TermQuery).Term()
                If objTerm.Field = FieldName Then
                    Dim fieldValue As String = BusinessController.GetTermText(objTerm)
                    For Each myList As DataListItem In lvListField.Items
                        Dim myCtl As LinkButton = DirectCast(myList.FindControl("lnkUnselectedField"), LinkButton)
                        If myCtl IsNot Nothing Then
                            If myCtl.CommandArgument = fieldValue Then
                                lvListField.SelectedIndex = myList.ItemIndex
                                lvListField.DataBind()
                                lnkReset.Enabled = True
                                Return True
                            End If
                        End If
                    Next
                End If
            End If
            Return False
        End Function


#End Region


#Region "Private methods"

        Private Sub BindLayout()
            Dim objListTermInfo As List(Of Business.TermInfo) = PortalQueryingDriver.GetCurrent().GetTopTerms(FieldName, NbValues)
            lnkReset.Visible = (objListTermInfo.Count <> 0)
            lvListField.DataSource = objListTermInfo
            If mySelectedIndex > -1 Then
                lvListField.SelectedIndex = mySelectedIndex
                lnkReset.Enabled = True
            End If
            lvListField.DataBind()
        End Sub


#End Region


    End Class

End Namespace

